<?php
namespace Admin\Model;
use Think\Model;
class OrderModel extends Model 
{
	protected $insertFields = array('member_id','total_price','pay_status','shr_name','shr_province','shr_city','shr_area','shr_address','shr_tel');
	protected $updateFields = array('id','member_id','total_price','pay_status','shr_name','shr_province','shr_city','shr_area','shr_address','shr_tel');
	protected $_validate = array(
		array('member_id', 'require', '会员id不能为空！', 1, 'regex', 3),
		array('member_id', 'number', '会员id必须是一个整数！', 1, 'regex', 3),
		array('total_price', 'require', '定单总价不能为空！', 1, 'regex', 3),
		array('total_price', 'currency', '定单总价必须是货币格式！', 1, 'regex', 3),
		array('pay_status', 'number', '支付状态必须是一个整数！', 2, 'regex', 3),
		array('shr_name', 'require', '收货人姓名不能为空！', 1, 'regex', 3),
		array('shr_name', '1,30', '收货人姓名的值最长不能超过 30 个字符！', 1, 'length', 3),
		array('shr_province', 'require', '收货人省不能为空！', 1, 'regex', 3),
		array('shr_province', '1,30', '收货人省的值最长不能超过 30 个字符！', 1, 'length', 3),
		array('shr_city', 'require', '收货人市不能为空！', 1, 'regex', 3),
		array('shr_city', '1,30', '收货人市的值最长不能超过 30 个字符！', 1, 'length', 3),
		array('shr_area', 'require', '收货人地区不能为空！', 1, 'regex', 3),
		array('shr_area', '1,30', '收货人地区的值最长不能超过 30 个字符！', 1, 'length', 3),
		array('shr_address', 'require', '收货人详细地址不能为空！', 1, 'regex', 3),
		array('shr_address', '1,150', '收货人详细地址的值最长不能超过 150 个字符！', 1, 'length', 3),
		array('shr_tel', 'require', '收货人电话不能为空！', 1, 'regex', 3),
		array('shr_tel', '1,30', '收货人电话的值最长不能超过 30 个字符！', 1, 'length', 3),
	);
	public function search($pageSize = 20)
	{
		/**************************************** 搜索 ****************************************/
		$where = array();
		if($member_id = I('get.member_id'))
			$where['member_id'] = array('eq', $member_id);
		if($addtime = I('get.addtime'))
			$where['addtime'] = array('eq', $addtime);
		$total_pricefrom = I('get.total_pricefrom');
		$total_priceto = I('get.total_priceto');
		if($total_pricefrom && $total_priceto)
			$where['total_price'] = array('between', array($total_pricefrom, $total_priceto));
		elseif($total_pricefrom)
			$where['total_price'] = array('egt', $total_pricefrom);
		elseif($total_priceto)
			$where['total_price'] = array('elt', $total_priceto);
		if($pay_status = I('get.pay_status'))
			$where['pay_status'] = array('eq', $pay_status);
		if($shr_name = I('get.shr_name'))
			$where['shr_name'] = array('like', "%$shr_name%");
		if($shr_province = I('get.shr_province'))
			$where['shr_province'] = array('like', "%$shr_province%");
		if($shr_city = I('get.shr_city'))
			$where['shr_city'] = array('like', "%$shr_city%");
		if($shr_area = I('get.shr_area'))
			$where['shr_area'] = array('like', "%$shr_area%");
		if($shr_address = I('get.shr_address'))
			$where['shr_address'] = array('like', "%$shr_address%");
		if($shr_tel = I('get.shr_tel'))
			$where['shr_tel'] = array('like', "%$shr_tel%");
		/************************************* 翻页 ****************************************/
		$count = $this->alias('a')->where($where)->count();
		$page = new \Think\Page($count, $pageSize);
		// 配置翻页的样式
		$page->setConfig('prev', '上一页');
		$page->setConfig('next', '下一页');
		$data['page'] = $page->show();
		/************************************** 取数据 ******************************************/
		$data['data'] = $this->alias('a')->where($where)->group('a.id')->limit($page->firstRow.','.$page->listRows)->select();
		return $data;
	}
	// 添加前
	protected function _before_insert(&$data, $option)
	{
	}
	// 修改前
	protected function _before_update(&$data, $option)
	{
	}
	// 删除前
	protected function _before_delete($option)
	{
		if(is_array($option['where']['id']))
		{
			$this->error = '不支持批量删除';
			return FALSE;
		}
	}
	/************************************ 其他方法 ********************************************/
}